package org.darkforest.merchant.service.impl;

import org.darkforest.common.services.MessageAndNoticeService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.dao.mapper.AddressMapper;
import org.darkforest.dao.mapper.OrderCommonMapper;
import org.darkforest.dao.mapper.OrderGoodsMapper;
import org.darkforest.dao.mapper.OrderMapper;
import org.darkforest.merchant.service.OrderService;
import org.darkforest.merchant.service.WXMessageService;
import org.darkforest.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by liu on 15/1/18.
 */
@Service("orderService")
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderCommonMapper orderCommonMapper;
    @Autowired
    private AddressMapper addressMapper;
//    @Autowired
//    private OrderService orderService;

    @Autowired
    private WXMessageService wXMessageService;

    @Autowired
    private MessageAndNoticeService messageAndNoticeService;

    @Autowired
    private OrderGoodsMapper orderGoodsMapper;

    @Override
    public void selectListByCondition(OrderSearchBean page) {
        page.setResultList(orderMapper.selectListByCondition(page));
    }

    @Override
    public OrderVo selectById(int id){
     return orderMapper.selectVoById(id);
    }

    @Override
    public void  updateByPrimaryKeySelective(Order order){
        orderMapper.updateByPrimaryKeySelective(order);
    }

    public OrderCommon getOrderCommonById(int orderId){
        return orderCommonMapper.selectByPrimaryKey(orderId);
    }

    public void updateOrderCommon(OrderCommon orderCommon){
        orderCommonMapper.updateByPrimaryKeySelective(orderCommon);
    }

    public List selectByMember(int id){
        return addressMapper.selectByMember(id);
    }

    @Override
    @Transactional
    public void deliverGoods(OrderCommon common, Order order) {
        common.setShippingTime(DateUtil.unixTimeStampInt().intValue());
        orderCommonMapper.updateByPrimaryKeySelective(common);
        orderMapper.updateByPrimaryKeySelective(order);
//        wXMessageService.setWXMessage(order.getOrderId());
        order = orderMapper.selectByPrimaryKey(order.getOrderId());
        messageAndNoticeService.orderDeliverSuccessToUser(order);

    }

    /****
     * 删除取消的订单
     * @param order
     */
    @Override
    @Transactional
    public void orderDel(Order order) {
        Order getOrder = orderMapper.selectByPrimaryKey(order.getOrderId());
        if(getOrder!=null&&getOrder.getOrderState().equals(order.getOrderState())){
            List<OrderGoods> list = orderGoodsMapper.findOrderGoodsListByOrderId(order.getOrderId());
            for(OrderGoods orderGoods:list){
                orderGoodsMapper.deleteByPrimaryKey(orderGoods.getRecId());
            }
            orderCommonMapper.deleteByPrimaryKey(order.getOrderId());
            orderMapper.deleteByPrimaryKey(order.getOrderId());
        }
    }

    @Override
    public void deleteOrderBatch(String[] orderIds) {
        for(String orderId:orderIds){
            Order getOrder = orderMapper.selectByPrimaryKey(Integer.parseInt(orderId));
            if(getOrder!=null&&getOrder.getOrderState().equals("0")){
                List<OrderGoods> list = orderGoodsMapper.findOrderGoodsListByOrderId(Integer.parseInt(orderId));
                for(OrderGoods orderGoods:list){
                    orderGoodsMapper.deleteByPrimaryKey(orderGoods.getRecId());
                }
                orderCommonMapper.deleteByPrimaryKey(Integer.parseInt(orderId));
                orderMapper.deleteByPrimaryKey(Integer.parseInt(orderId));
            }
        }

    }

}
